
const { addToDo,findToDoList,editToDoList,update } = require('../model/todoModel')
const { mongoose } = require('../utils/mongoose')
const moment=require('moment')
//新增
const addToDoList=async (req,res,next)=>{
    let {todo,date,id,type}=req.body;
    let obj={
        id,
        type,
        todo,
        status:'active',
        date,
        value:0,
        coin:0,
    }
    const result=await addToDo(obj)
    if(result){
        res.send({httpStatus:200,data:result,msg:'增加成功'})
    }else{
        res.send({httpStatus:400,data:[],msg:'增加出错'})
    }
}
//查询
const getToDoList = async (req, res) => {
    let {id,sDate,eDate}=req.body;
    const result = await findToDoList({id,sDate,eDate});
    if (result.length||result.length===0) {
        res.send({ httpStatus: 200, total: result.length, data: result })
    } else {
        res.send({ httpStatus: 400,  msg: '查询出错',date:[]})
    }
}
//操作
const operate = async (req, res) => {
    console.log(req.body)
    const { _id,id,status,value,coin,time, complexity} = req.body;
    const query = { id,_id };
    const updated = {
        status,
        value,
        coin
    }
    if(status==='completed'){
        updated.value=1;
        updated.coin=Math.ceil((time*complexity)/100/20);
    }

    if(status==='abandom'){
        updated.value=0;
        updated.coin=0;
    }

    const result = await editToDoList(query, updated)

    if (result.nModified !== 0) {
        res.send({ httpStatus: 200,  msg: '操作成功' })
    } else {
        res.send({ httpStatus: 400,  msg: '操作出错' })
    }

}



//添加
const addStu = async (req, res, next) => {
    const data = req.body;
    const result = await save(data);
    if (result) {
        res.send({ status: 200, state: true, msg: '增加成功', data: result })
    } else {
        res.send({ status: 400, state: false, msg: '增加信息出错' })
    }
}
//查询
const getStuList = async (req, res) => {
    const result = await find();
    if (result.length) {
        res.send({ status: 200, state: true, total: result.length, data: result })
    } else {
        res.send({ status: 400, state: false, msg: '查询出错' })
    }
}
//删除
const delStu = async (req, res) => {
    const { _id } = req.query;
    const query = { _id };
    const result = await del(query)
    if (result.n) {
        res.send({ status: 200, state: true, msg: '删除成功' })
    } else {
        res.send({ status: 400, state: false, msg: '删除出错' })
    }
}
//修改
const updateType = async (req, res) => {
    const { _id } = req.query;
    const query = { _id };
    const updated = req.body;
    const result = await update(query, updated)
    if (result.nModified !== 0) {
        res.send({ status: 200, state: true, msg: '修改成功' })
    } else {
        res.send({ status: 400, state: false, msg: '修改出错' })
    }

}
module.exports = {
    addToDoList,
    getToDoList,
    operate,
    updateType
}